package ru.pachkun.home.pages;

import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedConditions;

import ru.pachkun.home.block.AdvancedInformationRPOBlock;
import ru.pachkun.home.block.BasicInformationRPOBlock;
import ru.pachkun.home.block.ButtonFormBlock;
import ru.yandex.qatools.htmlelements.element.Select;
import ru.yandex.qatools.htmlelements.element.TextInput;

public class MRPORegistrationPage extends Page {

	public MRPORegistrationPage(PageManager pages) {
		super(pages);
	}

	@Override
	public MRPORegistrationPage ensurePageLoaded() {
		super.ensurePageLoaded();
		wait.until(presenceOfElementLocated(By.name("MRPOAddOperForm")));
		return this;
	}
	
	@FindBy(css="body.body")
	private BasicInformationRPOBlock basicInformationRPOBlock;
	
	@FindBy(css="body.body")
	private AdvancedInformationRPOBlock advancedInformationRPOBlock;
	
	@FindBy(css="body.body")
	private ButtonFormBlock buttonFormBlock;
	
	@FindBy(name = "mailTypeID")
	private Select mailTypeID;

	@FindBy(name = "transTypeID")
	private Select transTypeID;

	@FindBy(name = "aviaRate")
	private TextInput aviaRate;
	
	
	public ButtonFormBlock getButtonFormBlock() {
		return buttonFormBlock;
	}

	public BasicInformationRPOBlock getBasicInformationRPOBlock() {
		return basicInformationRPOBlock;
	}
	
	public AdvancedInformationRPOBlock getAdvancedInformationRPOBlock() {
		return advancedInformationRPOBlock;
	}
	
	public MRPORegistrationPage setMailTypeID(String text) {
		if (text == null) {
			return this;
		}
		mailTypeID.selectByVisibleText(text);
		return this;
	}

	public MRPORegistrationPage setTransTypeID(String text) {
		if (text == null) {
			return this;
		}
		transTypeID.selectByVisibleText(text);
		return this;
	}

	public MRPORegistrationPage setAviaRate(String text) {
		if (text == null) {
			return this;
		}
		aviaRate.sendKeys(text);
		return this;
	}

	public String getMailTypeID() {
		return mailTypeID.getFirstSelectedOption().getText();
	}

	public String getTransTypeID() {
		return transTypeID.getFirstSelectedOption().getText();
	}

	public String getAviaRate() {
		return aviaRate.getText();
	}
	
	public Boolean isErrorOnPage() {
		try {
			webDriver.findElement(By.cssSelector("p.page_ERROR"));
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}
	
}
